$(function(){
 getAdministorDate(1);
 
 

})
	var count;//总条数
	var PageSize=10;//设置每页示数目
    var PageCount;//计算总页数
    var index=1;//当前页，默认为1。

//获取表格
var content = $('.content_tr');//表格内容

function getAdministorDate(index){
	var alltr ='';
	$.ajax({
		url:'/api/admin/v1/alladminlist.action',
		type:'get',
		data:{index:parseInt(index)},
		success:function(data){
			 
			var thisdata =data.list;
			count = thisdata.count;//总条数
			PageCount=Math.ceil(count/PageSize);//总页数
			$('.pageNew').text(index);//第几页
			$('.pageCount').text(PageCount);

			content.empty();
			for(var i=0;i<thisdata.admins.length;i++){
				eachtr ="<tr><td>"+
				thisdata.admins[i].adminAid
				+"</td><td>"+
				thisdata.admins[i].adminAccount
				+"</td><td>"+
				thisdata.admins[i].adminPassword
				+"</td><td>"+
				thisdata.admins[i].adminCreationTime
				+"</td><td>"+
				thisdata.admins[i].adminLoginTime
				+"</td><td>"+
				thisdata.admins[i].adminLoginArea
				+"</td><td class='status' onclick=\'changeStatus("+thisdata.admins[i].adminAid+","+thisdata.admins[i].adminState+")\'>"+
				eachstatecolor(thisdata.admins[i].adminState)
				+"</td><td><i class='mdi mdi-delete sa-warning' onclick=\'deleteAccount("+thisdata.admins[i].adminAid+")'></i><i class='fa fa-edit' style='color: #007BB6;font-size: 17px;margin:0 5px' onclick='x_admin_show(\"修改信息\",\"changeAccount.html?id="+thisdata.admins[i].adminAid+"?name="+thisdata.admins[i].adminAccount+"?pass="+thisdata.admins[i].adminPassword+"\")'></i></td></tr>";
				 alltr+=eachtr;
				content.html(alltr);
				
			}

		}
	})
	
}
//修改状态颜色
function eachstatecolor(state){
	if (state==0) {
		return "<b style='color: green;'>未冻结</b>";
	}else{
		return "<b style='color: red;'>已冻结</b>";
	}
}
function changeStatus(id,state){
	var page = $('.pageNew').text();
	$.post('/api/admin/v1/updadmin.action',{aid:id,state:state==0?1:0},function(data){
		
			if(data.code == 0) {

		getAdministorDate(page);
		layer.msg('修改状态成功！');

		layer.msg(data.msg);
	} else {
		layer.msg('修改状态失败！');
	}
			
		
	})
	
}
//首页
$('.firstpage').click(function(){
	
	getAdministorDate(1);
	
})
//下一页
$('.nextPage').click(function(){
	index = index+1;
	if(index<=PageCount){
		getAdministorDate(index);
	}else{
		getAdministorDate(PageCount)
	}
	
	
	
})
//上一页
$('.previouspage').click(function(){
	index--;
	if(index<=1){
		getAdministorDate(1)
	}else{
		getAdministorDate(index)
	}
	
})
//尾页
$('.lastpage').click(function(){
	getAdministorDate(PageCount)
})
/*删除账号*/
function deleteAccount(id) {

	//$.post('/api/admin/v1/deladmin.action',{aid:id},function(data){

	swal({
		title: "你确定要删除嘛?",
		type: "warning",
		showCancelButton: !0,
		confirmButtonClass: "btn btn-success mt-2",
		cancelButtonClass: "btn btn-danger ml-2 mt-2",
		confirmButtonText: "确定",
		cancelButtonText: "取消",
		buttonsStyling: !1
	}).then(function(t) {
		t.value === true ? $.post('/api/admin/v1/deladmin.action', {
			aid: id
		}, function(data) {
			swal({
				title: "删除成功",
				text: "你的操作已生效",
				type: "success",
				confirmButtonClass: "btn btn-confirm mt-2"
			}).then(function() {

				if(data.code == 0) {
					parent.location.reload();

				}
			})

		}) : "cancel" === t.dismiss && swal({
			title: "操作取消",
			type: "error",
			timer: 2e3,
			confirmButtonClass: "btn btn-confirm mt-2"
		})

	})

}

function x_admin_show(title,url,id,w,h){
    if (title == null || title == '') {
        title=false;
    };
    if (url == null || url == '') {
        url="404.html";
    };
    if (w == null || w == '') {
        w=($(window).width()*0.9);
    };
    if (h == null || h == '') {
        h=($(window).height() + 500);
    };
    layer.open({
        type: 2,
        fix: false, //不固定
        maxmin: true,
        area: ['450px', '360px'], 
        shadeClose: true,
        shade:0.4,
        title: title,
        content: url,
        success:function(layero, index){
        	
        }
    });
}
function child(id){
	id=id;
}
/*关闭弹出框口*/
function x_admin_close(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}  


function updatestate(id,state) {
var str =$('.pageNew').text(); //当前页码值

	$.ajax({
		type: "post",
		url: "/api/admin/v1/updadmin.action",
		data: {
			aid: id,
			state: state==0?1:0
		},
		async: true,
		dataType: "json",
		success: function(data) {
		
		}
			
        

	});
}